<template>
	<view class="box">
		<view class="title">
			标题：
		</view>
		<view class="title-input">
			<up-input v-model="title" placeholder="请输入标题"></up-input>
		</view>
		<view class="content">
			内容：
		</view>
		<view class="content-input">
			<up-textarea v-model="content" placeholder="请输入内容" height="170" maxlength="-1" autoHeight></up-textarea>
		</view>
		<view class="btn">
			<up-button type="primary" text="保存" @click="save"></up-button>
		</view>
	</view>
</template>

<script>
	import tools from '../../tools';
	import { BASE_IP } from '@/common/http/url.js'
	export default {
		data() {
			return {
				user: null,
				id: 0,
				title: '',
				content: '',
			}
		},
		onLoad(e) {
			this.id = e.id;
			this.user = uni.getStorageSync("user")
			if (this.user.power != 1 && this.user.power != 2) {
				uni.reLaunch({ url: '/pages/user/users' });
			}
			this.getData();
		},
		methods: {
			getData() {
				uni.request({
					header: { "Authorization": this.user.token },
					method: "GET",
					url: BASE_IP + "pc/note/getNote/" + this.id,
					success: (res) => {
						console.log("详情：", res.data)
						if (res.statusCode == 200) {
							this.title = res.data.title;
							this.content = res.data.content;
						}
					}
				})
			},
			save() {
				if (this.title == '') {
					tools.showToast('标题不能空');
					return;
				}
				if (this.content == '') {
					tools.showToast('内容不能为空');
					return;
				}
				uni.request({
					header: { "Authorization": this.user.token },
					data: { id: this.id, title: this.title, content: this.content },
					method: 'POST',
					url: BASE_IP + "pc/note/save",
					success: (res) => {
						if (res.statusCode == 200) {
							uni.showToast({
								title: "保存成功",
								icon: "success"
							})
							setTimeout(() => {
								uni.navigateBack({ delta: 1 });
							}, 2000)
						}
						if (res.statusCode == 500) {
							uni.showToast({
								title: "添加失败",
								icon: "error"
							})
							return
						}
						if (res.statusCode == 404) {
							uni.showToast({
								title: "添加失败",
								icon: "error"
							})
							return
						}
					},
					fail: (res) => {
						console.log('s.f', res);
					}
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.box {
		padding: 30rpx;
	}

	.title,
	.content {
		font-weight: bold;
		margin: 20rpx 0;
	}

	.btn {
		margin: 20rpx 0;
	}
</style>